<?php
namespace app\admin\controller;
use app\service\adminService;
use think\Controller;
use think\Db;
class Login extends Controller
{
        public function login()
        {      
        $request=request();
            if($request->isGet()){
    
                return $this->fetch('login');
            }else{
                $data = $request->post();
                $validate = $this->validate($data,[
                        'name|名称'	=>  'require',
                        'password|密码'	=>  'require'
                ]);
                if($validate !== true){
                    echo "<script>alert('".$validate."');history.go(-1)</script>";
                    exit;
                    $this->error($validate);
                }
                if(!captcha_check(trim($data['captcha']))){
                    echo "<script>alert('验证码错误');history.go(-1)</script>";
                    exit;
                    $this->error('验证码错误');
                }
                $result = (new adminService())->login($data);
                if(!empty($result)){
                    session('id',$result['id']);
                    session('name',$result['name']);
                    session("usertype",$result['usertype']);
                    $this->redirect('admin/Index/index');
                }else{
                    echo "<script>alert('登陆失败');history.go(-1)</script>";
                    exit;
                    $this->error('登陆失败！');
                }
            }  
        }
        public function Logout(){
                session(null);
                $this->redirect(url('admin/login/login'));
        }

}